package com.test.daily.leetcode.y2022.m05.day0515.v03;

/**
 * @author Tom on 2022/5/15
 */
public class Solution {
    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);
        Node tmp = reverseNode(head);
//        while(tmp != null){
//            System.out.println(" " +tmp.val);
//            tmp = tmp.next;
//        }
        DoubleNode dNode1 = new DoubleNode(1);
        DoubleNode dNode2 = new DoubleNode(2);
        DoubleNode dNode3 = new DoubleNode(3);
        dNode1.last = null;
        dNode2.last = dNode1;
        dNode3.last = dNode2;
        dNode1.next = dNode2;
        dNode2.next = dNode3;
        DoubleNode dTmp = reverseDoubleNode(dNode1);
        while(dTmp != null){
            System.out.println(" " +dTmp.value);
            dTmp = dTmp.next;
        }

    }

    public static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public static class DoubleNode {
        public int value;
        public DoubleNode last;
        public DoubleNode next;

        public DoubleNode(int value) {
            this.value = value;
        }
    }

    public static DoubleNode reverseDoubleNode(DoubleNode head) {
        if (head == null) {
            return head;
        }
        DoubleNode last = null;
        DoubleNode next = null;
        while (head != null) {
            next = head.next;
            head.next = last;
            last = head;
            head.last = next;
            head = next;
        }
        return last;
    }

    public static Node reverseNode(Node head) {
        if (head == null) {
            return head;
        }
        Node pre = null;
        Node next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}
